package com.chinasoft.mall.activity;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chinasoft.mall.R;
import com.chinasoft.mall.activity.adapter.GoodsEmployeeAdapter;
import com.chinasoft.mall.activity.adapter.TopTopicAdapter;
import com.chinasoft.mall.activity.subActivity.goods.GoodsDetailActivity;
import com.chinasoft.mall.framework.BeanCache;
import com.chinasoft.mall.framework.Config;
import com.chinasoft.mall.framework.ControllerCenter;
import com.chinasoft.mall.model.BaseBean;
import com.chinasoft.mall.model.response.GoodsByClassEmployeeBean;
import com.chinasoft.mall.model.response.TopicBean;
import com.chinasoft.mall.widget.LoadListView;
import com.chinasoft.mall.widget.LoadListView.OnRefreshListener;
import com.chinasoft.mall.widget.NetImageView;
import com.chinasoft.mall.widget.NetShadowImageView;

public class StaffGoodsClassify extends BaseActivity implements OnClickListener, OnRefreshListener {

	private ViewPager gallery;

	private int track = 0;

	private MyPagerAdapter mpa;

	private List<EpgViewInfo> listViews;

	private TopTopicAdapter giAdapter;

	private LayoutInflater mInflater; // Tab页面列表

	// 从表RECHN中读取，默认按发布日期排序。
	private BaseBean rechn;

	private BaseBean topic;

	private List<TopicBean> _topic;

	private LinearLayout line_ll;
	private int choiceId = 0;

	// 下拉列表
	private LoadListView modellist;
	private BaseBean sp_bb;
	private GoodsEmployeeAdapter mlAdapter;
	private List<GoodsByClassEmployeeBean> gcbs;
	//将所属分类的商品全部显示出来
	private BaseBean goodsbyclass_bb;

	private String goodsbyclasstitle;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.staff_goods_classify);
		initTabView();
		
		initGoodsListView();

	}

	private void initGoodsListView() {
		modellist=(LoadListView) findViewById(R.id.modellist);
		mlAdapter=new GoodsEmployeeAdapter((MainActivity)this.getParent(), null);
		modellist.setAdapter(mlAdapter);
		modellist.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				GoodsByClassEmployeeBean scd = gcbs.get(position);
				if (scd != null) {
					goodsbyclasstitle = scd.getCLASS_NAME();
					goodsbyclass_bb = new BaseBean(Config.GET_GOODSBYCLASSID);
					goodsbyclass_bb.reqData.put("class_id",  scd.getCLASS_ID());
					goodsbyclass_bb.reqData.put("pageNo","1");
					control.sendMessage(goodsbyclass_bb);
				}
			}
		});
		
	}

	private void initTabView() {
		gallery = (ViewPager) findViewById(R.id.galleryid);
		gallery.setOnClickListener(this);

		listViews = new ArrayList<EpgViewInfo>();
		mpa = new MyPagerAdapter(listViews);

		giAdapter = new TopTopicAdapter((MainActivity) this.getParent(),
				null);
		mInflater = getLayoutInflater();
		line_ll = (LinearLayout) findViewById(R.id.staff_ll);

		
		
		gallery.setAdapter(mpa);
		gallery.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
				// TODO Auto-generated method stub
				track = arg0;
				for (int i = 0; i < line_ll.getChildCount(); i++) {
					if (i == track) {
						((ImageView) line_ll.getChildAt(i))
								.setImageResource(R.drawable.blackpoint);
					} else {
						((ImageView) line_ll.getChildAt(i))
								.setImageResource(R.drawable.graypoint);
					}
				}
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {

			}
		});

	}

	@Override
	public void onClick(View v) {

	}

	public class EpgViewInfo {
		View view;
		TopicBean eb;
	}

	@Override
	protected void onResume() {
		super.onResume();
		initRequest();
	}

	@Override
	public void success(BaseBean obj) {
		super.success(obj);
		if (obj.equals(topic)) {
			_topic = (List<TopicBean>) obj.getResData().getData();
			if (_topic != null && !_topic.equals(giAdapter.getData())) {
				giAdapter.setData(_topic);

				for (TopicBean eb : _topic) {
					EpgViewInfo vi = new EpgViewInfo();
					vi.view = mInflater.inflate(R.layout.imagegv, null);
					vi.eb = eb;
					listViews.add(vi);
				}

				
				line_ll.removeAllViews();
				for (int i = 0; i < _topic.size(); i++) {
					ImageView point = (ImageView) LayoutInflater.from(this)
							.inflate(R.layout.point, null);
					
//					_ll.removeAllViews();
//					for (int i = 0; i < gidAdapter.getCount(); i++) {
//						ImageView point = (ImageView) LayoutInflater.from(this)
//								.inflate(R.layout.point, null);
//						_ll.addView(point, i);
//					}
					
//					ImageView line_iv = (ImageView) _ll
//							.findViewById(R.id.line_iv);
//					_ll.removeView(line_iv);
					line_ll.addView(point, i);
				}
				gallery.setCurrentItem(choiceId);
			}
		} else if (obj.equals(sp_bb)) {
			gcbs = (List<GoodsByClassEmployeeBean>) obj.getResData().getData();
			mlAdapter.setData(gcbs);
		} else if (obj.equals(goodsbyclass_bb)) {
			((MainActivity) this.getParent()).setCurrentActivity(
					StaffGoodsListActivity.class, goodsbyclasstitle, obj);
		}

	}

//	private List<TopicBean> getTopicTop() {
//		List<TopicBean> _ebs = LivingGoods.getInstance().getLiving_ebs();
//		if (_ebs.size() >= 5) {
//			return _ebs.subList(0, 5);
//		} else {
//			choiceId = 0;
//			LinkedList<EpgBean> _lebs = new LinkedList<EpgBean>(_ebs);
//			List<EpgBean> wait_ebs = LivingGoods.getInstance().getWait_ebs();
//			List<EpgBean> lived_ebs = LivingGoods.getInstance().getLived_ebs();
//			int waitid = 0;
//			int waitsize = wait_ebs.size();
//			int lived = lived_ebs.size();
//			boolean isLeft = true;
//			for (int i = 0; i < 5 - _ebs.size(); i++) {
//				if (waitid < waitsize && isLeft) {
//					_lebs.addLast(wait_ebs.get(waitid++));
//					isLeft = false;
//				} else if (lived > 0 && !isLeft) {
//					choiceId++;
//					_lebs.addFirst(lived_ebs.get(--lived));
//					isLeft = true;
//				} else {
//					if (waitid < waitsize) {
//						_lebs.addLast(wait_ebs.get(waitid++));
//					} else if (lived > 0) {
//						choiceId++;
//						_lebs.addFirst(lived_ebs.get(--lived));
//					}
//					isLeft = !isLeft;
//				}
//			}
//			// gallery.setSelection(choiceId);
//			return _lebs;
//		}
//	}

	private void initRequest() {
		topic = BeanCache.getInstance().getResponsdata().get(Config.EMPLOYEE_GOODS_TOPIC);

		if (topic != null) {
			success(topic);
			control.postMessage(topic);
		} else {
			topic = new BaseBean(Config.EMPLOYEE_GOODS_TOPIC);
			topic.setUrl_type((byte) 1);
			control.sendMessage(topic);
		}
		sp_bb = BeanCache.getInstance().getResponsdata().get(Config.EMPLYEE_GOODS_CLASS);
		if (sp_bb != null) {
			success(sp_bb);
		} else {
			sp_bb = new BaseBean(Config.EMPLYEE_GOODS_CLASS);
			sp_bb.setUrl_type((byte) 1);
			control.sendMessage(sp_bb);
		}
	}

	/**
	 * ViewPager适配器
	 */
	public class MyPagerAdapter extends PagerAdapter {

		public List<EpgViewInfo> mListViews;

		public MyPagerAdapter(List<EpgViewInfo> mListViews) {
			this.mListViews = mListViews;
		}

		public void setmListViews(List<EpgViewInfo> mListViews) {
			this.mListViews = mListViews;
			this.notifyDataSetChanged();
		}

		@Override
		public void destroyItem(View arg0, int arg1, Object arg2) {
			((ViewPager) arg0).removeView(mListViews.get(arg1).view);
		}

		@Override
		public void finishUpdate(View arg0) {
		}

		@Override
		public int getCount() {
			return mListViews.size();
		}

		@Override
		public Object instantiateItem(View arg0, int arg1) {
			View v = mListViews.get(arg1).view;
			NetImageView _niv = ((NetImageView) v.findViewById(R.id.netimage));
			_niv.setDownload(TopicBean.TOPIC_URL
					+ mListViews.get(arg1).eb.getTP_ID()+"/"+mListViews.get(arg1).eb.getTPIC());
			final TopicBean _eb = mListViews.get(arg1).eb;
			_niv.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					((MainActivity) StaffGoodsClassify.this.getParent())
							.setCurrentActivity(StaffTopGoodsActivity.class,
									_eb.getTP_NAME(), _eb);
				}
			});
			((ViewPager) arg0).addView(v, 0);
			return v;
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == (arg1);
		}

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1) {
		}

		@Override
		public Parcelable saveState() {
			return null;
		}

		@Override
		public void startUpdate(View arg0) {
		}
	}

	class ViewHolder {
		public NetShadowImageView gl_1;
		public TextView tv_1;
		public NetShadowImageView gl_2;
		public TextView tv_2;
		public NetShadowImageView gl_3;
		public TextView tv_3;
	}


	@Override
	public void onRefresh() {
		goodsbyclass_bb.reqData.put("pageNo", (Integer.parseInt(goodsbyclass_bb.reqData.get("pageNo"))+1)+"");
		control.postMessage(goodsbyclass_bb);

	}

}
